var app = getApp();
const { $Toast } = require('../../dist/base/index');
var server = require('../../utils/server');
Page({
	data: {
		filterId: 1,
		searchWords: '',
		placeholder: '烤鸭',
		shops: app.globalData.shops,
    shopList:true,
    searchStatus:false,
    imgUrl:'',
	},
	onLoad: function () {
		var self = this;
    self.setData({
      imgUrl: wx.getStorageSync('imgUrl')
    })
    this.setData({
      imgUrl:wx.getStorageSync('imgUrl')
    })
		wx.getLocation({
			type: 'gcj02',
			success: function (res) {
		    
			}
		})
	},
	onShow: function () {
		//this.setData({
		//	showResult: false
		//});
	},
	inputSearch: function (e) {
		this.setData({
			searchWords: e.detail.value
		});
	},
  format(arr){
    var flag = "";
    if (!arr[0]) {
      flag = "无满减活动";
    } else {
      var flag = "";
      for (var i = 0; i < arr.length; i++) {

        let item = "满" + arr[i].meet + "减" + arr[i].minus + ",";
        flag += item;
      }
      console.log(flag)
      return flag;


    }
  },
	doSearch: function() {
  let that=this;
    // 
    if (!this.data.placeholder){
      this.handleWarning("内容不能为空")
    }else{
      this.handleLoading();
      server.postReq('/mobile/home/linkName',{
        userId:wx.getStorageSync('uid'),
       name: that.data.searchWords,
       commodityId:'',
     },function(res){
       var newobj=res.data.data;
       for(var i in newobj){
         newobj[i].mm = that.disc(newobj[i].meetMinus);
       }
     
       setTimeout(() => {
         $Toast.hide();
         that.setData({
           shops: newobj,
           shopList: true,
           searchStatus: false
         })
     });
     
          // this.handleWarning("结果为空")
      }, 1000);
      this.setData({
        showResult: true
      });
    }
		
	},
  disc(arr) {
    var flag = "";
    if (Object.prototype.toString.call(arr).split(' ')[1].split(']')[0] != 'Array') {
      flag = "无满减活动";
    } else {

      for (var i = 0; i < arr.length; i++) {
        let item = "满" + arr[i].meet + "减" + arr[i].minus + ",";
        flag += item;

      }
    }
    return flag;
  },
	tapFilter: function (e) {
		switch (e.target.dataset.id) {
			case '1':
				this.data.shops.sort(function (a, b) {
					return a.id > b.id;
				});
				break;
			case '2':
				this.data.shops.sort(function (a, b) {
					return a.sales < b.sales;
				});
				break;
			case '3':
				this.data.shops.sort(function (a, b) {
					return a.distance > b.distance;
				});
				break;
		}
		this.setData({
			filterId: e.target.dataset.id,
			shops: this.data.shops
		});
	},
  handleSuccess() {
    $Toast({
      content: '成功的提示',
      type: 'success'
    });
  },
  handleWarning(msg) {
    $Toast({
      content: msg,
      type: 'warning'
    });
  },
  handleError() {
    $Toast({
      content: '错误的提示',
      type: 'error'
    });
  },
  handleLoading() {
    $Toast({
      content: '加载中',
      type: 'loading',
      duration:0,
    });
  },
});

